package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 获取菜单列表
     *
     * @return
     */
    @GetMapping("/menuList")
    public Result menuList() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Menu> menus = menuService.findMenuByUsername(user.getUsername());
        return new Result(true, MessageConstant.GET_MENU_SUCCESS, menus);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, "新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增菜单失败");
        }
    }

    /**
     * 添加子菜单
     *
     * @param menu
     * @param roleIds
     * @return
     */
    @PostMapping("/addSon")
    public Result addSon(@RequestBody Menu menu, @RequestParam("roleIds") Integer[] roleIds) {
        try {
            menuService.addSon(menu, roleIds);
            return new Result(true, "新增子菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增子菜单失败");
        }
    }

    @GetMapping("/findAllRole")
    public Result findAllRole() {
        try {
            List<Role> allRole = menuService.findAllRole();
            return new Result(true, "获取所有角色成功", allRole);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取所有角色失败");
        }
    }

    /**
     * 根据menuId获取菜单信息
     *
     * @return
     */
    @GetMapping("/findMenuById/{id}")
    public Result findMenuById(@PathVariable Integer id) {
        Menu menu = menuService.findMenuById(id);
        return new Result(true, "获取菜单信息成功", menu);
    }

    /**
     * 编辑菜单，回显权限信息
     *
     * @param id
     * @return
     */
    @GetMapping("findRole/{id}")
    public Result findRole(@PathVariable Integer id) {
        Integer[] role = menuService.findRole(id);
        return new Result(true, "获取角色信息成功", role);
    }

    /**
     * 修改菜单
     *
     * @param menu
     * @param roleIds
     * @return
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody Menu menu, @RequestParam("roleIds") Integer[] roleIds) {
        try {
            menuService.update(menu, roleIds);
            return new Result(true, "修改菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改菜单失败");
        }
    }

    /**
     * 根据菜单id删除菜单
     *
     * @param menuId
     * @return
     */
    @DeleteMapping("/{menuId}")
    public Result delete(@PathVariable Integer menuId) {
        try {
            menuService.delete(menuId);
            return new Result(true, "删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "删除菜单失败");
        }
    }
}
